var express = require('express');
var path = require('path');
var router = express.Router();
var mysql = require('./pool');
router.get('*', function (req, res) {
    var page = 'yeeor.html';
    switch (req.url) {
        case '/':
            page = 'lyjindex.html';
            break;
        case '/telLogin':
            page = 'lyjtelLogin.html';
            break;
        case '/psdLogin':
            page = 'lyjpsdLogin.html';
            break;
        case '/register':
            page = 'lyjregister.html';
            break;
        case '/forget':
            page = 'lyjforget.html';
            break;
        case '/alert':
            page = 'lyjalert.html';
            break;
        case '/search1':
            page = 'lyjsearch1.html';
            break;
        case '/search2':
            page = 'lyjsearch2.html';
            break;
        case '/goods':
            page = 'lyjgoods.html';
            break;
        case '/Friday':
            page = 'lyjFriday.html';
            break;
        case '/jifengshop':
            page = 'lxcjifenshangcheng.html';
            break;
        case '/city':
            page = 'ytcfw.html';
            break;
        case '/goodsxq':
            page = 'jfspxq.html';
            break;
        case '/Mycount':
            page = 'lwbmyaccount.html';
            break;
        case '/shopxq':
            page = 'lbwxq.html';
            break;
    }
    let pathStr = path.normalize(__dirname + `\/../views/${page}`);
    res.sendFile(pathStr);
});
//首页商品详情
var allDate = []
router.post('/alldata', async function (req, res) {
    allDate.length = 0;
    if (req.body.nav) {
        let cateDate = await mysql('select * from category');
        if (cateDate.length > 0) {
            allDate.push(cateDate);
            res.send(allDate);
        }
    } else {
        let cateDate = await mysql('select * from category');
        let goodinfo = await mysql('select * from goods where is_new = 1 or is_hot = 1 limit 4');
        let goodsOne = await mysql('select * from goods where cat_id = 1');
        let goodsTwo = await mysql('select * from goods where cat_id = 2');
        let goodsThree = await mysql('select * from goods where cat_id = 3');
        let goodsFour = await mysql('select * from goods where cat_id = 4');
        // let swipergoods = await mysql('select * from goods where friactive = 1');
        if (cateDate.length > 0 && goodinfo.length > 0 && goodsOne.length > 0) {
            allDate.push(cateDate, goodinfo, goodsOne, goodsTwo, goodsThree, goodsFour);
            res.send(allDate);
        }
    }

})
//列表
var arrList = [];
router.post('/lyjcataID', async function (req, res) {
    arrList.length = 0
    let data = req.body;
    let children = await mysql('select * from children where partentid =' + data.cataID);
    let goods = await mysql('select * from goods where cat_id = ' + data.cataID)
    if (children.length > 0 && goods.length > 0) {
        arrList.push(children, goods);
        res.send(arrList);
    }
})
//搜索商品
router.post('/search', async function (req, res) {
    console.log(req.body);
    let search = await mysql("select * from goods where goods_name like '%" + req.body.name + "%'");
    if (search.length == 0) {
        res.send('0');
    } else if (search.length > 0) {
        if (req.body.lyjIndex) {
            res.send(search);
        } else {
            res.send('1');
        }

    }
})
module.exports = router;